﻿using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine.BrainInfo;
using SCG.General;

namespace SCG.SolarVengeanceEngine
{
    //Represents a wormhole that sends StarShips to a different location on the map
    public class Wormhole : SVMapObject
    {
        //constructors
        public Wormhole(SVGame game)
            : base(game)
        {
            game.Wormholes.Add(this);
            foreach (Player p in game.Players)
                _traveledBy[p] = false;
        }

        //Remove it
        public override void Remove()
        {
            base.Remove();
            Game.Wormholes.Remove(this);
        }

        //Destination of wormhole
        public int DestX { get; set; }

        public int DestY { get; set; }
        
        public SCGPoint Destination
        {
            get
            {
                return new SCGPoint(DestX, DestY);
            }
        }

        //Was the wormhole traveled by a player?
        public IDictionary<Player, bool> TraveledBy
        {
            get
            {
                return _traveledBy;
            }
        }

        //Is it a JumpGate?
        public bool IsJumpGate { get; set; }

        //persistence
        public override string Persist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.Tokenize(DestX, DestY, IsJumpGate);
            foreach (Player p in Game.Players)
                tok.AppendToken(TraveledBy[p]);
            return base.Persist() + tok.Result;
        }
        public override SVTokenizer Parse(string s)
        {
            SVTokenizer tok = base.Parse(s);
            DestX = tok.TokenInt;
            DestY = tok.TokenInt;
            IsJumpGate = tok.TokenBoolean;
            foreach (Player p in Game.Players)
                TraveledBy[p] = tok.TokenBoolean;
            return tok;
        }

        //Private members      
        private Dictionary<Player, bool> _traveledBy = new Dictionary<Player, bool>();
        private WormholeInfo _info = null;

        //Access info object for Brains
        internal WormholeInfo Info
        {
            get
            {
                if (_info == null)
                    _info = new WormholeInfo(this);
                return _info;
            }
        }
    }
}
